
#include "app.h"
#include "x_nucleo_iks01a1_hum_temp.h"
#include "x_nucleo_iks01a1_pressure.h"
#include "x_nucleo_iks01a1_imu_6axes.h"
#include "hts221.h"
#include "lps25hb.h"
#include "lps25h.h"
#include "lsm6ds3.h"
#include "lsm303agr.h"

static void app_sensor_check(void);
static void app_led_en_pin_config(uint8_t reg);
/*start adv*/

char *name = "CAF_ECHO";
uint8_t adv_address[6] = {0x08, 0x05, 0x04, 0x03, 0x02, 0x04};

void on_ready(void)
{
    uint8_t tx_power_level = 7;
    uint16_t adv_interval = 100;

    /*Config Adv Parameter And Ready to Adv*/
//    ble_set_adv_param(name, adv_address, tx_power_level, adv_interval);
//    ble_device_start_advertising();
    /*check sensor*/
    app_sensor_check();
}

/*init hts221*/
static void hum_temp_monitor_init(void)
{
    BSP_HUM_TEMP_Init();
    /* Initialize the HUM temp */
    while(BSP_HUM_TEMP_isInitialized() != 1) {
        BSP_HUM_TEMP_Init();
        HAL_Delay(10);
    }
}

/*init lps25hb*/
static void pressure_sensor_init(void)
{
    BSP_PRESSURE_Init();
    while((BSP_PRESSURE_isInitialized() != 1)) {
        BSP_PRESSURE_Init();
        HAL_Delay(10);
    }
}

/*init lsm6ds3*/
static void lsm6ds3_6_axis_init(void)
{
    BSP_IMU_6AXES_Init();
    while(BSP_IMU_6AXES_isInitialized() != 1)	{
        BSP_IMU_6AXES_Init();
        HAL_Delay(10);
    }
}

/*init lsm303agr*/
static void lsm303agr_init(void)
{
    int8_t ret;
    ret = init_LSM303AGR_mag(LSM303AGR_MAG_ODR_100Hz);
    while(ret == -1)
    {

    }
 
}

static void app_sensor_check(void)
{
    uint8_t ret,tmp1 = 0x55;
 
    /*init*/
//    hum_temp_monitor_init();
//    pressure_sensor_init();
//    lsm6ds3_6_axis_init();
//    lsm303agr_init();
    if(HTS221_IO_Init() != HUM_TEMP_OK)
    {
       while(1);
    }
    /*check hts221*/
    ret = BSP_HUM_TEMP_CheckID();
    if(ret != APP_OK)
    {
        while(1);
    }
    /*check lps25hb*/
    ret =BSP_PRESSURE_CheckID();
    if(ret != APP_OK)
    {
        while(1);
    }   
    /*check ls6ds3*/
    ret = BSP_IMU_6AXES_Check_XG_ID();
    if(ret != APP_OK)
    {
        while(1);
    } 
    /*check lsm303agr*/
    ret = LSM303AGR_MAG_R_WHO_AM_I(&tmp1);
    if(ret != APP_OK)
    {
        while(1);
    }
    if(tmp1 != LSM303AGR_MAG_WHO_AM_I)
    {
        while(1);
    }
    
    app_led_en_pin_config(0x01);
    HAL_Delay(500);
    app_led_en_pin_config(0x00);
    
}

static void app_led_en_pin_config(uint8_t reg)
{
    if(reg == 0x00){
        HAL_GPIO_WritePin(GPIOC, GPIO_PIN_12, GPIO_PIN_RESET); 
    }else{
        HAL_GPIO_WritePin(GPIOC, GPIO_PIN_12, GPIO_PIN_SET); 
    }
}

/* Device On Message */
void ble_device_on_message(uint8_t type, uint16_t length, uint8_t* value)
{

    /*echo data*/
    ble_device_send(type, length, value);

}
/* Device on connect */
void ble_device_on_connect(void)
{


}
/* Device on disconnect */
void ble_device_on_disconnect(uint8_t reason)
{
    /* Make the device connectable again. */
    Ble_conn_state = BLE_CONNECTABLE;
    ble_device_start_advertising();
}
